// pages/account/account.js
var app = getApp()
var enryption = require("../../utils/encryption.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: '',
    id: '',
    list: '',
    items: [{
        name: '设计部',
        value: '设计部'
      },
      {
        name: '业务部',
        value: '业务部'
      },
      {
        name: '采购部',
        value: '采购部'
      },
      {
        name: '工厂',
        value: '工厂'
      },
      {
        name: '仓库',
        value: '仓库'
      },
      {
        name: '管理员',
        value: '管理员'
      },
    ],
    jurisdiction: [],
    factoryGroup: [{
      groupName: '请选择',
      id: ''
    }],
    factoryGroupIdnex: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    this.getFactoryGroup()

    this.setData({
      type: options.type
    })
    if (options.id != undefined) {
      this.setData({
        id: options.id
      })
      this.user()
    }

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  formSubmit: function(e) {
    if (e.detail.value.mobile == '' || e.detail.value.realName == '') {
      wx.showToast({
        title: '请填写完整信息',
      })
      return
    }

    e.detail.value.password = enryption.md5(e.detail.value.password);
    if (this.data.type == "子管理员" && this.data.id == '') {
      wx.request({
        url: app.globalData.api + 'user/addSonAdmin',
        data: {
          mobile: e.detail.value.mobile,
          password: e.detail.value.password,
          realName: e.detail.value.realName,
          roleName: this.data.type,
          remarks: e.detail.value.remarks,
          canOperating: this.data.jurisdiction.join(',')
        },
        method: 'POST',
        header: {
          Authorization: wx.getStorageSync('token')
        },
        success: function(res) {
          console.log(res)
          if (res.data.code == 200) {
            wx.showToast({
              title: res.data.msg,
            })
            setTimeout(() => {
              wx.navigateBack({
                delta: 1
              })
            }, 1000)
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.errorMsg,
            })
          }
        }
      })
      return
    }
    if (this.data.id == '') {
      wx.request({
        url: app.globalData.api + 'user/add',
        data: {
          mobile: e.detail.value.mobile,
          password: e.detail.value.password,
          realName: e.detail.value.realName,
          roleName: this.data.type,
          address: e.detail.value.address,
          remarks: e.detail.value.remarks,
          factoryGroupId: this.data.factoryGroup[this.data.factoryGroupIdnex].id
        },
        method: 'POST',
        header: {
          Authorization: wx.getStorageSync('token')
        },
        success: function(res) {
          console.log(res)
          if (res.data.code == 200) {
            wx.showToast({
              title: res.data.msg,
            })
            setTimeout(() => {
              wx.navigateBack({
                delta: 1
              })
            }, 1000)
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.errorMsg,
            })
          }
        }
      })
    } else {
      wx.request({
        url: app.globalData.api + 'user/' + this.data.id,
        data: {
          mobile: e.detail.value.mobile,
          password: e.detail.value.password,
          realName: e.detail.value.realName,
          roleName: this.data.type,
          address: e.detail.value.address,
          remarks: e.detail.value.remarks,
          canOperating: this.data.jurisdiction.join(','),
          factoryGroupId: this.data.factoryGroup[this.data.factoryGroupIdnex].id
        },
        method: 'PUT',
        header: {
          Authorization: wx.getStorageSync('token')
        },
        success: function(res) {
          console.log(res)
          if (res.data.code == 200) {
            wx.showToast({
              title: res.data.msg,
            })
            setTimeout(() => {
              wx.navigateBack({
                delta: 1
              })
            }, 1000)
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.errorMsg,
            })
          }
        }
      })
    }

  },
  user: function() {
    var that = this
    wx.request({
      url: app.globalData.api + 'user/' + this.data.id,
      data: {},
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success: function(res) {
        console.log(res)
        if (res.data.code == 200) {
          if (that.data.type == "子管理员") {
            for (let i in that.data.items) {
              for (let j in res.data.data.canOperating.split(',')) {
                if (that.data.items[i].name == res.data.data.canOperating.split(',')[j]) {
                  that.data.items[i].checked = true
                }
              }
            }
            that.setData({
              jurisdiction: res.data.data.canOperating.split(','),
              items: that.data.items
            })
          }
          let index = 0,
            factory = [];
          if (res.data.data.factoryGroupId != null && res.data.data.factoryGroupId != '') {
            for (let i in that.data.factoryGroup) {
              factory.push(that.data.factoryGroup[i].id)
            }
            index = factory.indexOf(res.data.data.factoryGroupId)
          }


          console.log()
          that.setData({
            list: res.data.data,
            factoryGroupIdnex: index
          })
        }
      }
    })

  },
  checkboxChange(e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    this.setData({
      jurisdiction: e.detail.value
    })
  },
  getFactoryGroup() {
    var that = this
    wx.request({
      url: app.globalData.api + 'factoryGroup/list',
      data: {
        rows: 99
      },
      method: 'POST',
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success(res) {
        console.log(res)
        if (res.data.code == 200) {
          for (let i of res.data.data.factoryGroupList) {
            that.data.factoryGroup.push(i)
          }
          console.log(that.data.factoryGroup)
          that.setData({
            factoryGroup: that.data.factoryGroup
          })
        }
      }
    })
  },
  bindPickerChange: function(e) {
    this.setData({
      factoryGroupIdnex: e.detail.value
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})